﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameEngine
{
    [Serializable]
    public class Room
    {

        #region Attributes

        private bool _active;
        private Floor _floor;

        private List<Door> _doors;
        private List<DecorationLayer> _background;
        private List<DecorationLayer> _foreground;
        private List<GameTrigger> _triggers;

        private Rectangle _roomBoundary;

        #endregion

        #region Constructors

        public Room(Rectangle roomBoundary, float scale, Texture2D floorTexture, Texture2D collisionTexture, Vector2 floorLocation)
        {
            this._active = false;
            this._floor = new Floor(floorTexture, floorLocation, scale, collisionTexture);
            this._roomBoundary = roomBoundary;
            this._doors = new List<Door>();
            this._background = new List<DecorationLayer>();
            this._foreground = new List<DecorationLayer>();
            this._triggers = new List<GameTrigger>();
        }

        public Room(Texture2D floorTexture, Texture2D collisionTexture, Vector2 floorLocation)
        {
            this._active = false;
            this._floor = new Floor(floorTexture, floorLocation, 1, collisionTexture);
            this._roomBoundary = new Rectangle(0, 0, collisionTexture.Width, collisionTexture.Height);

            this._doors = new List<Door>();
            this._background = new List<DecorationLayer>();
            this._foreground = new List<DecorationLayer>();
            this._triggers = new List<GameTrigger>();
        }

        #endregion

        #region Properties

        public Rectangle RoomBoundary
        {
            set { this._roomBoundary = value; }
            get { return this._roomBoundary; }
        }

        public bool Active
        {
            set { this._active = value; }
            get { return this._active; }
        }

        public Floor Floor
        {
            get { return this._floor; }
        }

        public List<Door> Doors
        {
            get { return this._doors; }
        }

        public List<DecorationLayer> Background
        {
            get { return this._background; }
        }

        public List<DecorationLayer> Foreground
        {
            get { return this._foreground; }
        }

        public List<GameTrigger> Triggers
        {
            get { return this._triggers; }
        }

        #endregion

        #region Public Methods

        public void addBackgroundLayer(DecorationLayer newLayer)
        {
            Background.Add(newLayer);
        }

        public void removeBackgroundLayer(int index)
        {
            Background.RemoveAt(index);
        }

        public void addForegroundLayer(DecorationLayer newLayer)
        {
            Foreground.Add(newLayer);
        }

        public void removeForegroundLayer(int index)
        {
            Foreground.RemoveAt(index);
        }

        public void addDoor(Door newDoor)
        {
            Doors.Add(newDoor);
        }

        public void addTrigger() { }

        public void removeTrigger() { }

        public void Update(GameTime gameTime, Vector2 playerLocation)
        {

            foreach (Layer layer in Background)
            {
                layer.Update(gameTime, Vector2.Zero);
            }

            Floor.Update(gameTime, Vector2.Zero);

            foreach (Layer layer in Foreground)
            {
                layer.Update(gameTime, Vector2.Zero);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                foreach (Layer layer in _background)
                {
                    layer.Draw(spriteBatch);
                }

                Floor.Draw(spriteBatch);



                foreach (Layer layer in _foreground)
                {
                    layer.Draw(spriteBatch);
                }

            }
        }

        #endregion

    }
}
